Skip to content
created by Aha00aAha00a at 2021-04-20
last modified by Aha00aAha00a at 2021-08-05
revision: 3
console.log(`${__filename}:1`);

const promiseMysql = require("promise-mysql");

const hostRw = process.env.AWS_RDS_HOST_RW;
const hostRo = process.env.AWS_RDS_HOST_RO;
const user = process.env.AWS_RDS_USER;
const password = process.env.AWS_RDS_PASSWORD;
const database = process.env.AWS_RDS_DATABASE;

const createPool = host => {
    let pool;
    const poolPromise = async () => {
        if (pool)
            return pool;

        console.log(`createPool\t${host}`);
        return pool = promiseMysql.createPool({
            host,
            user,
            password,
            database,
            ssl: {
                rejectUnauthorized: false,
                minVersion: 'TLSv1',
            },
            typeCast: function castField(field, useDefaultTypeCasting) {
                if ((field.type === "BIT") && ( /*MySQL*/ field.length === 1 || /*MariaDB*/ field.columnLength === 1)) {
                    const bytes = field.buffer();
                    if (bytes === null)
                        return null;

                    return bytes[0] === 1;
                }
                return useDefaultTypeCasting();
            },
        });
    };

    return {
        getPoolPromise: async () => await poolPromise(),
        async getConnection() {
            return (await poolPromise()).getConnection(...arguments)
        },
        async query() {
            return (await poolPromise()).query(...arguments)
        },
        async selectOne() {
            const array = await (await poolPromise()).query(...arguments);
            return array?.[0];
        },
        selectOneViaConn: async (conn, ...rest) => {
            const array = await conn.query(...rest);
            return array?.[0];
        },
    };
};

module.exports = {
    rw: createPool(hostRw),
    ro: createPool(hostRo),
};

--2021-08-04


console.log(`${__filename}:1`);

const promiseMysql = require("promise-mysql");

let pool;
const poolPromise = async () => {
    if (pool)
        return pool;

    console.log('createPool');
    // noinspection JSUnusedGlobalSymbols
    pool = promiseMysql.createPool({
        ssl: {
            rejectUnauthorized: false,
        },
        host : process.env.DB_HOST,
        database : process.env.DB_DATABASE,
        user : process.env.DB_USER,
        password : process.env.DB_PASSWORD,
        typeCast: function castField(field, useDefaultTypeCasting) {
            if ((field.type === "BIT") && ( /*MySQL*/ field.length === 1 || /*MariaDB*/ field.columnLength === 1)) {
                const bytes = field.buffer();
                if(bytes === null)
                    return null;

                return bytes[0] === 1;
            }
            return useDefaultTypeCasting();
        },
    });

    return pool;
};

const headOrNull = array => {
    // return array?.[0]; // node 14 부터 가능
    if (!array || !array.length)
        return null;

    return array[0];
};

// noinspection JSUnusedGlobalSymbols
module.exports = {
    getPoolPromise: async () => await poolPromise(),
    query: async function () {
        return (await poolPromise()).query(...arguments)
    },
    getConnection: async function () {
        return (await poolPromise()).getConnection(...arguments)
    },
    selectOne: async function () {
        const array = await (await poolPromise()).query(...arguments);
        return headOrNull(array);
    },
    selectOneViaConn: async function (conn, ...rest) {
        const array = await conn.query(...rest);
        return headOrNull(array);
    },
};

--2021-04-20

1. See Also

1.2. Similar Pages

Similar pages by cosine similarity. Words after page name are term frequency.

1.3. Adjacent Pages

Control
≤ 32
all
1.0x
1.0x
80
-120
ON
Metrics
Nodes(visible/total)0/0
Links(visible/total)0/0
Avg degree0.00
Depth coverage0
Queue(fetch/graph)0 / 0
Zoom(scale)1.00x
Ctrl/⌘ + Scroll: Zoom
Root 1-hop 2-hop+